<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row layui-col-space5">
                <div class="layui-col-md3">
                    <div class="layui-card-header">标签组</div>
                    <table class="layui-hide"  id="left-table" lay-filter="left-table"></table>
                </div>
                <div class="layui-col-md9">
                    <div class="layui-card-header">标签管理</div>
                    <table class="layui-hide"  id="main-table" lay-filter="main-table"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<script th:inline="none"></script>
<script>
    layui.use(['table','form','element'], function(){
        var table = layui.table,
            element = layui.element,
            form = layui.form;
        table.render({
            elem: '#left-table'
            ,height: (top.height-85)+'px'
            ,url: 'tagGroup/getPageList'
            ,title: '标签组列表'
            ,page: true
            ,cols: [
                [
                    {type: 'checkbox'}
                    ,{field: 'name', title: "组名",width:280}
                ]
            ]
            ,done:function(){
                //enableRowClick(table,form,'left-table',"left-table");
            }
        });
        table.on('row(left-table)', function(obj){
            clickLayTableRow(table,form,'left-table',obj);
            table.reload("main-table",{where:{groupId:obj.data.id}});
        });
        table.render({
            elem: '#main-table'
            ,height: (top.height-85)+'px'
            ,url: 'tag/getPageList'
            ,title: '标签列表'
            ,toolbar: '#toolbar'
            ,page: true
            ,cols: [
                [
                    {type: 'checkbox'}
                    ,{field: 'name', title: "标签名称",width:280,templet:function(d){
                        return "<a class='colum-link' href='javascript:void(0)' onclick='edit(\""+d.id+"\")'>" + d.name + "</a>";
                    }}
                    ,{field: 'orderno',title: "排序",width:120,edit:'text'}
                    ,{field: 'createUserName',title: "登记人",width:120}
                    ,{field: 'createTime', title: "登记时间",width:170}
                    ,{field: 'stateTxt',title: "状态",width:90}
                ]
            ]
            ,done:function(){
                enableRowClick(table,form,'main-table',"main-table");
            }
        });
        table.on('edit(main-table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            axios.post('tag/save',obj.data).then(
                res=>{
                    layer.msg('保存成功',{icon:1});
                    table.reload('main-table');
                }
            )
        });
        //头工具栏事件
        table.on('toolbar(main-table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    var leftStatus = table.checkStatus('left-table');
                    if(leftStatus.data.length==0){

                        return layer.msg("请选择标签组");
                    }
                    layer.open({
                        id:"openwin",
                        type: 2,
                        title:'增加标签',
                        area:['1100px','600px'],
                        content:  'rooter?url=base/tag/add-tag&groupId=' + leftStatus.data[0].id,
                        btn:['保存','关闭'],
                        yes:function(index,layero){
                            var dataForm = layer.getChildFrame('form', index);
                            dataForm.find("#submitBtn").click();
                            return false
                        }
                    });
                    break;
                case 'delete':
                    var data = checkStatus.data;
                    if(data.length>0){
                        var values = {};
                        var ids=[];
                        $.each(data,function(i,v){
                            ids.push(v.id);
                        })
                        values['ids'] = ids.join(",");
                        layer.confirm('确定要删除当前行吗?', {icon: 3, title:'提示'}, function(index){
                            axios.post("tag/delete",values).then(res=>{
                                if (res.data.code == 1) {
                                    layer.msg(res.msg);
                                } else {
                                    layer.close(index);
                                    table.reload('main-table');
                                    layer.msg("删除成功。");
                                }
                            })
                        });
                    }else{
                        layer.msg("请选择操作数据!")
                    }
                    break;
                case 'edit':
                    var data2 = checkStatus.data;
                    if(data2.length>0){
                        edit(data2[0].id);
                    }else{
                        layer.msg("请选择操作数据！")
                    }
                    break;
            };
        });
        var $ = layui.$, active = {
            query: function(){

                table.reload("main-table",{where:{name:$("#name").val()}})
            },
            resetBtn:function () {
                $("#btReset").click();

            }
        };
        window.loadGrid = function(tableName){
            table.reload(tableName);
        }
        window.queryQuestion=function (id,name){
            localStorage.setItem("libid",id);
            top.$("#29").click();
        }
        $('.layui-form .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
    function edit(id){
        layer.open({
            id:"openwin",
            type: 2,
            title:'修改标签组',
            area:['800px','600px'],
            content:  'rooter?url=base/tag/add-tag&id='+id,
            btn:['保存','关闭'],
            yes:function(index,layero){
                var dataForm = layer.getChildFrame('form', index);
                dataForm.find("#submitBtn").click();
                return false
            }
        });
    }

</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-addition"></i>增加</button>
        <button class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>
    </div>
</script>
</body>
</html>